﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;

namespace WallCreateAndEdit.WallModule.SearchExternalLoop
{
	public class MapNode
	{
		public XYZ Position
		{
			get
			{
				return this.m_ptPosition;
			}
		}

		public List<MapEdge> EdgeList
		{
			get
			{
				return this.m_EdgeList;
			}
		}

		public void AddEdge(MapEdge Edge)
		{
			this.m_EdgeList.Add(Edge);
		}

		public void RemoveEdge(int index)
		{
			if (index > 0 && index < this.m_EdgeList.Count)
			{
				this.m_EdgeList.RemoveAt(index);
				this.AnticlockwiseSortElements();
			}
		}

		public void AnticlockwiseSortElements()
		{
			this.m_EdgeList.Sort(new AngleComparer(this.m_ptPosition, 0.0));
		}

		public int FindLinkEdgeWithOtherNode(XYZ ptOtherNode)
		{
			List<int> list = new List<int>();
			for (int i = 0; i < this.m_EdgeList.Count; i++)
			{
				MapEdge mapEdge = this.m_EdgeList[i];
				XYZ xyz = mapEdge.CurEndPoint(this.m_ptPosition);
				if (xyz != null && ptOtherNode.IsAlmostEqualTo(xyz, 0.0001))
				{
					list.Add(i);
				}
			}
			if (list.Count < 1)
			{
				return -1;
			}
			return list[list.Count - 1];
		}

		public MapNode(MapNode rhs)
		{
			this.m_ptPosition = rhs.m_ptPosition;
			this.m_EdgeList = rhs.m_EdgeList;
		}

		public MapNode(XYZ ptPosition, List<MapEdge> EdgeList)
		{
			this.m_ptPosition = ptPosition;
			this.m_EdgeList = EdgeList;
		}

		private XYZ m_ptPosition;

		private List<MapEdge> m_EdgeList = new List<MapEdge>();
	}
}
